﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;


namespace HighScore.Web
{
    [ServiceContract(Namespace = "")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        dcHighScoreDataContext db = new dcHighScoreDataContext();
     

        [OperationContract]
        public void DoWork()
        {
            // Add your operation implementation here
            return;
        }

        [OperationContract]
        public List<HighScore> GetHighScore(int appId)
        {
            var result =from s in db.HighScores orderby s.Score descending where s.AppId == appId select s;

            

            List<HighScore> mijnhighscores = result.ToList();

            

            return mijnhighscores;

        }

        [OperationContract]
        public HighScore InsertHighScore(int appId, int score, int openleuvenId)
        {
            HighScore mijnhighscore = new HighScore();
            mijnhighscore.AppId = appId;
            mijnhighscore.Date = System.DateTime.Now;
            mijnhighscore.Score = score;
            mijnhighscore.OpenLeuvenId = openleuvenId;

            db.HighScores.InsertOnSubmit(mijnhighscore);
            db.SubmitChanges();

            return mijnhighscore;
        }

        [OperationContract]
        public HighScore InsertHighScoreWedstrijd(int appId, int score, int openleuvenId, int wedstrijdId)
        {
            HighScore mijnhighscore = new HighScore();
            mijnhighscore.AppId = appId;
            mijnhighscore.Date = System.DateTime.Now;
            mijnhighscore.WedstrijdId = wedstrijdId;
            mijnhighscore.OpenLeuvenId = openleuvenId;
            mijnhighscore.Score = score;

            db.HighScores.InsertOnSubmit(mijnhighscore);
            db.SubmitChanges();

            return mijnhighscore;
        }

        [OperationContract]
        public HighScore EditHighscoreWedstrijd(int appId, int score, int openleuvenId, int wedstrijdId)
        {
            List<HighScore> highscore = this.GetHighScore(appId);

            foreach (HighScore player in highscore)
            {
                if (player.OpenLeuvenId == openleuvenId)
                {
                    if (player.Score < score)
                    {
                        db.HighScores.DeleteOnSubmit(player);
                        player.Score = score;
                        player.Date = System.DateTime.Now;

                        db.HighScores.InsertOnSubmit(player);
                        db.SubmitChanges();
                        return player;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            return null;
        }

        [OperationContract]
        public HighScore EditHighscore(int appId, int score, int openleuvenId)
        {
            var result = from s in db.HighScores where s.AppId == appId select s;

            foreach (var player in result)
            {
                if (player.OpenLeuvenId == openleuvenId)
                {
                    
                    if (player.Score < score)
                    {
                        db.HighScores.DeleteOnSubmit(player); 
                        player.Score = score;
                        player.Date = System.DateTime.Now;

                        db.HighScores.InsertOnSubmit(player);
                        db.SubmitChanges();
                        return player;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            return null;
        }
        // Add more operations here and mark them with [OperationContract]
    }
}
